package com.lyz.dataStructure.algorithm.五八同城2020;/**
 * @Author:759057893@qq.com Lyz
 * @Date: ${Date} 20:54
 * @Description:
 **/

import java.util.Arrays;
import java.util.Scanner;

/**
 *@Author:759057893@qq.com Lyz
 *@Date: 2019/9/12 20:54
 *@Description:
 **/
public class 分饼干 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n =sc.nextInt();
        int[] arr = new int[n];
        for(int i =0;i<n;i++){
            arr[i] = sc.nextInt();
        }
        System.out.println(candy(arr));
    }
    public static int candy(int[] ratings) {
        int A[] = new int[ratings.length];
        Arrays.fill(A, 1);
        for (int i = 1; i < ratings.length; i++)
            if (ratings[i] > ratings[i - 1])
                A[i] = A[i - 1] + 1;
        int sum = A[ratings.length - 1];
        for (int i = ratings.length - 2; i >= 0; i--) {
            if (ratings[i] > ratings[i + 1] && A[i] <= A[i + 1])
                A[i] = A[i + 1] + 1;
            sum += A[i];
        }
        return sum;

    }
}
